<?php

namespace App\Console\Commands;

use DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use App\Http\Models\ProductInfo;

class Add_Billing extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Add:Billing';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '会社の請求書を追加';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \Log::info('会社の請求書を追加　Start');
//        $ProductInfo = new ProductInfo();
//        $price = $ProductInfo->getPrice();
        $memberPrice = config('businessVolume.MEMBER_PRICE');
        $Price = $memberPrice + ($memberPrice * config('businessVolume.SALE_TAX'));
        $tmp_year_month = date('Ym', strtotime('-1 month'));
        DB::table('request_success')->insert(array(
            'request_year_month' =>$tmp_year_month,
            'created_at' =>now(),
        ));
        Log::Info('Add Billing bath add');
        $userCompanys = DB::table('user_company')->select('user_company.id', 'user_company.User_Company_Type')->get();
        $tmp_date=date("Ym");
        //年を取得する
        $tmp_year=substr($tmp_date,0,4);
        //月を取得する
        $tmp_mon =substr($tmp_date,4,2);
        $tmp_nextmonth=mktime(0,0,0,$tmp_mon+1,1,$tmp_year);
        $tmp_nextnextmonth=mktime(0,0,0,$tmp_mon+2,1,$tmp_year);
        //来月を取得する
        $fm_next_month=date("Y-m-1",$tmp_nextmonth);
        $fm_nextnext_month=date('Y-m-d', strtotime(date('Y-m-01', strtotime($fm_next_month)) . ' +1 month -1 day'));
        $num = date("Ym");
        if (sizeof($userCompanys) > 0) {
            for ($i = 0; $i < count($userCompanys); $i++) {
                $id = $userCompanys[$i]->id;
                $Type = $userCompanys[$i]->User_Company_Type;
                if ($Type) {
                    $string = '請求予定';
                } else {
                    $string = '不請求';
                }
                $num1 = '';
                if ($id <= 9) {
                    $num1 = $num.'-000000000'.$id;
                } else if ($id <= 99) {
                    $num1 = $num.'-00000000'.$id;
                } else if ($id <= 999) {
                    $num1 = $num.'-0000000'.$id;
                } else if ($id <= 9999) {
                    $num1 = $num.'-000000'.$id;
                } else if ($id <= 99999) {
                    $num1 = $num.'-00000'.$id;
                } else if ($id <= 999999) {
                    $num1 = $num.'-0000'.$id;
                } else if ($id <= 9999999) {
                    $num1 = $num.'-000'.$id;
                } else if ($id <= 99999999) {
                    $num1 = $num.'-00'.$id;
                } else if ($id <= 999999999) {
                    $num1 = $num.'-0'.$id;
                } else {
                    $num1 = $num.'-'.$id;
                }
                Log::debug($num1);
                DB::table('billing')->insert(
                    ['sum_num' => 0,
                    'total_price' => $Price,
                    'total_money' => $Price,
                    'status' => $string,
                    'billing_year_month' => $fm_next_month,
                    'billing_date' => $fm_nextnext_month,
                    'create_user_id' => 1,
                    'member_price' => $memberPrice,
                    'update_user_id' => 1,
                    'User_Company_id' => $id,
                    'billing_number' => $num1]
                );
            }
        }
        \Log::info('会社の請求書を追加　End');
    }
}
